package cc.fs.zhihuapp.ui.activity.demo;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.App;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.News;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.ui.adapter.MyViewPagerAdapter;
import cc.fs.zhihuapp.utils.EncodeUtil;
import cc.fs.zhihuapp.utils.PxUtils;
import cc.fs.zhihuapp.utils.QuickReturnUtils;

/**
 * Created by fostion on 2015/9/17.
 */
public class MyViewPagerActivity extends BaseActivity {


    @InjectView(R.id.recyclerView)
    RecyclerView recyclerView;
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.baseMsg)
    TextView baseMsg;
    @InjectView(R.id.introdution)
    TextView introdution;
    @InjectView(R.id.linearlayout1)
    LinearLayout linearlayout1;
    @InjectView(R.id.chatBtn)
    TextView chatBtn;
    @InjectView(R.id.commentBtn)
    TextView commentBtn;
    @InjectView(R.id.dateBtn)
    TextView dateBtn;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.coordinatorLayout)
    RelativeLayout coordinatorLayout;

    MyViewPagerAdapter adapter;
    @InjectView(R.id.headLayout)
    LinearLayout headLayout;
    private int hidenHeight;
    private int needAnimateOffset;
    private ValueAnimator titlebarTextColorAnimator;
    private boolean toolbarShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.acitivity_my_toolbar_layout);
        ButterKnife.inject(this);

        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setTitle("电影");
        setSupportActionBar(toolbar);

        chatBtn.setOnClickListener(new MySelector());
        commentBtn.setOnClickListener(new MySelector());
        dateBtn.setOnClickListener(new MySelector());

        ArrayList<News> news = new ArrayList<>();
        news.add(new News(true, "1"));
        news.add(new News(true, "2"));
        news.add(new News(true, "3"));
        news.add(new News(true, "4"));
        news.add(new News(true, "5"));
        news.add(new News(true, "6"));
        news.add(new News(true, "7"));
        news.add(new News(true, "8"));
        news.add(new News(true, "9"));
        news.add(new News(true, "10"));
        news.add(new News(true, "11"));
        news.add(new News(true, "12"));
        news.add(new News(true, "13"));
        news.add(new News(true, "14"));
        news.add(new News(true, "15"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));
        news.add(new News(true, "16"));

        L(EncodeUtil.MD5("post"));
        App.instance.getDatabaseHelper().saveUser("1","fostion","http://www.baidu.com");
        L(App.instance.getDatabaseHelper().getUser("1"));


        //animator
        titlebarTextColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(),0x00588dba , 0xff588dba);
        titlebarTextColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                toolbar.setBackgroundColor((Integer) animation.getAnimatedValue());
            }
        });

        adapter = new MyViewPagerAdapter(news);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int moveY = -QuickReturnUtils.getScrollY(recyclerView, 1);
                needAnimateOffset = -toolbar.getHeight();
                hidenHeight = toolbar.getHeight() - headLayout.getHeight();
                if (moveY < hidenHeight) {
                    moveY = hidenHeight;
                }

                if (moveY < needAnimateOffset && !toolbarShow) {//启动动画显示toolbar
                    titlebarTextColorAnimator.start();
                    toolbarShow = true;
                } else if (moveY > needAnimateOffset && toolbarShow) {
                    titlebarTextColorAnimator.reverse();
                    toolbarShow = false;
                }

                linearlayout1.setTranslationY(moveY);
            }

        });
    }

    public void hiddenToolBar(){
        titlebarTextColorAnimator.reverse();
        toolbarShow = false;
    }

    @Override
    public void handler(Message msg) {

    }

    class MySelector implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v.getId() == chatBtn.getId()) {
                chatBtn.setSelected(true);
                commentBtn.setSelected(false);
                dateBtn.setSelected(false);
            }
            if (v.getId() == commentBtn.getId()) {
                chatBtn.setSelected(false);
                commentBtn.setSelected(true);
                dateBtn.setSelected(false);
            }
            if (v.getId() == dateBtn.getId()) {
                chatBtn.setSelected(false);
                commentBtn.setSelected(false);
                dateBtn.setSelected(true);
            }

            if (linearlayout1.getTranslationY() != 0) {//解决viewpager滑动有空白及其他bug具体参考微影项目
                recyclerView.scrollToPosition(0);
                ObjectAnimator anim = ObjectAnimator.ofFloat(linearlayout1, "translationY", linearlayout1.getTranslationY(), 0);
                anim.setDuration(200);
                anim.start();
                hiddenToolBar();
            }
        }
    }
}
